package com.centanet.framework.glide;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.BitmapTypeRequest;
import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;

/**
 * glide图片加载
 */
public final class GlideLoad {

    public static boolean loadWithWifi;//只在wifi下加载
    public static boolean isWifi;//是否wifi网络

    private GlideLoad() {
        // Utility class.
    }

    /**
     * 初始化
     *
     * @param activity Activity
     * @return DrawableRequestBuilder<String>
     */
    public static DrawableRequestBuilder<String> init(Activity activity) {
        return Glide.with(activity)
                .fromString()
                .skipMemoryCache(Build.VERSION.SDK_INT < 21)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .clone();
    }

    /**
     * 初始化
     *
     * @param fragment Fragment
     * @return DrawableRequestBuilder<String>
     */
    public static DrawableRequestBuilder<String> init(Fragment fragment) {
        return Glide.with(fragment)
                .fromString()
                .skipMemoryCache(Build.VERSION.SDK_INT < 21)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .clone();
    }

    /**
     * 初始化
     *
     * @param activity Activity
     * @return DrawableRequestBuilder<Uri>
     */
    public static DrawableRequestBuilder<Uri> initUri(Activity activity) {
        return Glide.with(activity)
                .fromUri()
                .skipMemoryCache(Build.VERSION.SDK_INT < 21)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .clone();
    }

    /**
     * 初始化圆形加载
     *
     * @return
     */
    public static BitmapTypeRequest<String> initRound(Activity activity) {
        return Glide
                .with(activity)
                .from(String.class)
                .asBitmap();
    }

    /**
     * 初始化圆形加载
     *
     * @return
     */
    public static BitmapTypeRequest<String> initRound(Fragment fragment) {
        return Glide
                .with(fragment)
                .from(String.class)
                .asBitmap();
    }

    /**
     * 加载图片
     *
     * @param builder {@link com.centanet.framework.glide.GlideLoad.Builder}
     */
    public static void load(@NonNull final Builder builder) {
        if (needLoad(builder.checkWifiSwitch)) {
            if (builder.fitCenter) {
                builder.requestBuilder
                        .load(builder.url)
                        .placeholder(builder.placeholder)
                        .error(builder.error)
                        .listener(new LogListener<String, GlideDrawable>())
                        .fitCenter()
                        .into(builder.imageView);
            } else {
                builder.requestBuilder
                        .load(builder.url)
                        .placeholder(builder.placeholder)
                        .error(builder.error)
                        .listener(new LogListener<String, GlideDrawable>())
                        .centerCrop()
                        .into(builder.imageView);
            }
        } else {
            if (builder.fitCenter) {
                builder.requestBuilder
                        .load("")
                        .placeholder(builder.placeholder)
                        .error(builder.error)
                        .listener(new LogListener<String, GlideDrawable>())
                        .fitCenter()
                        .into(builder.imageView);
            } else {
                builder.requestBuilder
                        .load("")
                        .placeholder(builder.placeholder)
                        .error(builder.error)
                        .listener(new LogListener<String, GlideDrawable>())
                        .centerCrop()
                        .into(builder.imageView);
            }
        }
    }

    /**
     * 加载图片[根据{@link Transformation}改变图片显示样式]
     *
     * @param builder               {@link com.centanet.framework.glide.GlideLoad.Builder}
     * @param bitmapTransformations {@link Transformation}
     */
    @SafeVarargs
    public static void loadTransformation(@NonNull final Builder builder, Transformation<Bitmap>... bitmapTransformations) {
        if (needLoad(builder.checkWifiSwitch)) {
            builder.requestBuilder
                    .load(builder.url)
                    .placeholder(builder.placeholder)
                    .error(builder.error)
                    .listener(new LogListener<String, GlideDrawable>())
                    .bitmapTransform(bitmapTransformations)
                    .into(builder.imageView);
        } else {
            builder.requestBuilder
                    .load("")
                    .placeholder(builder.placeholder)
                    .error(builder.error)
                    .listener(new LogListener<String, GlideDrawable>())
                    .bitmapTransform(bitmapTransformations)
                    .into(builder.imageView);
        }
    }

    /**
     * 加载大图
     *
     * @param builder {@link com.centanet.framework.glide.GlideLoad.Builder}
     */
    public static void loadBig(@NonNull final Builder builder) {
        if (needLoad(builder.checkWifiSwitch)) {
            final boolean thumbnailEnable = !TextUtils.isEmpty(builder.thumbnailUrl);//缩略图可用
            DrawableRequestBuilder<String> thumbnailRequest = builder.requestBuilder.clone();
            thumbnailRequest.load(thumbnailEnable ? builder.thumbnailUrl : "");
            if (builder.fitCenter) {
                if (thumbnailEnable) {
                    builder.requestBuilder
                            .load(builder.url)
                            .listener(new LogListener<String, GlideDrawable>())
                            .fitCenter()
                            .thumbnail(thumbnailRequest)
                            .into(builder.imageView);
                } else {
                    builder.requestBuilder
                            .load(builder.url)
                            .placeholder(builder.placeholder)
                            .error(builder.error)
                            .listener(new LogListener<String, GlideDrawable>())
                            .fitCenter()
                            .into(builder.imageView);
                }
            } else {
                if (thumbnailEnable) {
                    builder.requestBuilder
                            .load(builder.url)
                            .listener(new LogListener<String, GlideDrawable>())
                            .centerCrop()
                            .thumbnail(thumbnailRequest)
                            .into(builder.imageView);
                } else {
                    builder.requestBuilder
                            .load(builder.url)
                            .placeholder(builder.placeholder)
                            .error(builder.error)
                            .listener(new LogListener<String, GlideDrawable>())
                            .centerCrop()
                            .into(builder.imageView);
                }
            }
        } else {
            if (builder.fitCenter) {
                builder.requestBuilder
                        .load("")
                        .fallback(builder.error)
                        .listener(new LogListener<String, GlideDrawable>())
                        .fitCenter()
                        .into(builder.imageView);
            } else {
                builder.requestBuilder
                        .load("")
                        .fallback(builder.error)
                        .listener(new LogListener<String, GlideDrawable>())
                        .centerCrop()
                        .into(builder.imageView);
            }
        }
    }

    /**
     * 是否需要加载
     *
     * @param checkWifiSwitch 关联wifi开关
     * @return boolean
     */
    public static boolean needLoad(boolean checkWifiSwitch) {
        return !(checkWifiSwitch && loadWithWifi) || isWifi;
    }

    /**
     * 构建{@link #load(Builder)}、{@link #loadTransformation(Builder, Transformation[])}加载参数
     */
    public static class Builder {
        private final DrawableRequestBuilder<String> requestBuilder;
        private final String url;
        private String thumbnailUrl;
        private int placeholder = 0;
        private int error = 0;
        private boolean fitCenter;
        private ImageView imageView;
        private boolean checkWifiSwitch;

        /**
         * @param requestBuilder {@link DrawableRequestBuilder}
         * @param url            图片url
         */
        public Builder(DrawableRequestBuilder<String> requestBuilder, String url) {
            this.requestBuilder = requestBuilder;
            this.url = url;
        }

        /**
         * 缩略图url
         *
         * @param thumbnailUrl 缩略图url只在{@link #loadBig(Builder)}有效
         * @return Builder
         */
        public Builder thumbnailUrl(String thumbnailUrl) {
            this.thumbnailUrl = thumbnailUrl;
            return this;
        }

        /**
         * 位图
         *
         * @param placeholderId 位图id
         * @return Builder
         */
        public Builder placeHolder(@DrawableRes int placeholderId) {
            this.placeholder = placeholderId;
            return this;
        }

        /**
         * 加载出错显示图片
         *
         * @param error 错误图片id
         * @return Builder
         */
        public Builder error(@DrawableRes int error) {
            this.error = error;
            return this;
        }

        /**
         * 启用fitCenter,默认centerCrop
         *
         * @return Builder
         */
        public Builder fitCenter() {
            fitCenter = true;
            return this;
        }

        /**
         * @param imageView ImageView
         * @return Builder
         */
        public Builder into(ImageView imageView) {
            this.imageView = imageView;
            return this;
        }

        /**
         * 打开关联wifi开关
         *
         * @return Builder
         */
        public Builder openWifiSwitch() {
            checkWifiSwitch = true;
            return this;
        }
    }
}
